package com.tle.learnandroidlifetime;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity 
{

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // Properties //
    ////////////////////////////////////////////////////////////////////////////////////////////////////
	protected LinearLayout m_LayoutMain = null;
	protected Button m_btnSubActivity1 = null;
	protected Button m_btnSubActivity2 = null;
    ////////////////////////////////////////////////////////////////////////////////////////////////////
																				   		// Properties //
    ////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {    	
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onCreate");
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//        if (savedInstanceState == null) 
//        {
//            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
//        }
                
        this.initGUI();
        this.registerListener();
    }

    @Override
    protected void onStart() 
    {
    	// TODO Auto-generated method stub
		Utilities.showToast(this, MainActivity.class.getSimpleName() + " onStart");
    	
    	super.onStart();
    }
    
    @Override
    protected void onResume() 
    {
    	// TODO Auto-generated method stub
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onResume");
    	super.onResume();
    }
    
    @Override
    protected void onPause() 
    {
    	// TODO Auto-generated method stub
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onPause");
    	super.onPause();
    }
    
    @Override
    protected void onStop() 
    {
    	// TODO Auto-generated method stub
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onStop");
    	super.onStop();
    }
    
    @Override
    protected void onDestroy() 
    {
    	// TODO Auto-generated method stub
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onDestroy");
    	super.onDestroy();
    }
    
    @Override
    protected void onRestart() 
    {
    	// TODO Auto-generated method stub
    	Utilities.showToast(this, MainActivity.class.getSimpleName() + " onRestart");
    	super.onRestart();
    }
    
    
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // Support Methods //
    ////////////////////////////////////////////////////////////////////////////////////////////////////        
    protected void initGUI ()
    {
    	m_LayoutMain = new LinearLayout(this);
    	m_LayoutMain.setOrientation(LinearLayout.HORIZONTAL);
    	m_LayoutMain.setGravity(Gravity.CENTER_HORIZONTAL);
    	
    	this.setContentView(m_LayoutMain);
    	
    	
    	
    	LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT , 1);
    	m_btnSubActivity1 = new Button(this);
    	m_btnSubActivity1.setText("Sub Activity 1");
    	m_btnSubActivity1.setLayoutParams(layoutParams1);
    	
    	m_btnSubActivity2 = new Button(this);
    	m_btnSubActivity2.setText("Sub Activity 2");
    	m_btnSubActivity2.setLayoutParams(layoutParams1);
    	
    	
    	
    	m_LayoutMain.addView(m_btnSubActivity1);
    	m_LayoutMain.addView(m_btnSubActivity2);
    }
    
    protected void registerListener ()
    {
    	ButtonEvent buttonEvent = new ButtonEvent();
    	m_btnSubActivity1.setOnClickListener(buttonEvent);
    	m_btnSubActivity2.setOnClickListener(buttonEvent);
    }
    
    private class ButtonEvent implements OnClickListener
    {
    	@Override
    	public void onClick(View v) 
    	{
    		// TODO Auto-generated method stub
    		if (v == m_btnSubActivity1)
    		{
    			Intent intent = new Intent(MainActivity.this, SubActivity1.class);
    			startActivity(intent);
    		}
    		else if (v == m_btnSubActivity2)
    		{
    			Intent intent = new Intent(MainActivity.this, SubActivity2.class);
    			startActivity(intent);
    		}
    		
    	}
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    																			   // Support Methods //
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // Override Methods //
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    																			  // Override Methods //
    ////////////////////////////////////////////////////////////////////////////////////////////////////
}
